package com.atguigu.juc.day03.jmm;

import java.util.concurrent.CountDownLatch;

/**
 * volatile关键词并不能解决原子性问题
 */
public class Demo04VolatileAtomic {

    private volatile int number = 0;

    public void doAdd() {
        number++;
    }

    public int getNumber() {
        return number;
    }

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(2);

        Demo04VolatileAtomic demo = new Demo04VolatileAtomic();

        new Thread(()->{
            for (int i = 0; i < 10000000; i++) {
                demo.doAdd();
            }
            countDownLatch.countDown();
        }, "thread-a").start();

        new Thread(()->{
            for (int i = 0; i < 10000000; i++) {
                demo.doAdd();
            }
            countDownLatch.countDown();
        }, "thread-b").start();

        countDownLatch.await();
        int numberCurrent = demo.getNumber();
        System.out.println("numberCurrent = " + numberCurrent);

    }

}
